﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Extensions;
using System.Net;
using Core.Smtp;
using System.Text.RegularExpressions;

namespace Core
{
    public class ProtocolLine : Line
    {
        public int Serial { get; set; }
        public IPAddress Ip { get; set; }
        public SmtpStatusCode StatusCode { get; set; }
        public SmtpDirection Direction { get; set; }
        public String Command { get; set; }

        public ProtocolLine(String rawLine, String[] columns)
            : base(rawLine, columns)
        {
        }

        protected override void Parse(String[] columns)
        {
            ThreadId = columns[1].ParseInt();
            Serial = columns[2].ParseInt();
            TimeStamp = columns[3].ParseDateTime();
            Ip = columns[4].ParseIpAddress();
            Text = columns[5];

            Match m = LineTemplates.ProtocolMessage.Match(Text);
            Direction = (m.Groups["SentOrReceived"].Value == "SENT") ? SmtpDirection.Sent : SmtpDirection.Received;
            Command = m.Groups["Command"].Value;
            int statusCode = 0;
            int.TryParse(m.Groups["StatusCode"].Value, out statusCode);
            StatusCode = (SmtpStatusCode)statusCode;
        }

        public override void Chain(Chainer chainer)
        {
            chainer.AppendToChain(this);
        }
    }
}
